// TodoController.java
package com.mytools.project.todo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mytools.config.AjaxResult;
import com.mytools.project.todo.entity.Todo;
import com.mytools.project.todo.req.TodoReq;
import com.mytools.project.todo.resp.TodoTaskOutResp;
import com.mytools.project.todo.service.TodoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/todos")
@CrossOrigin(origins = "*") // 允许跨域访问
@Slf4j
public class TodoController {
    
    @Autowired
    private TodoService todoService;
    
    /**
     * 查询待办事项列表
     */
    @GetMapping
    public AjaxResult listTodos(
            @RequestParam(required = false) String userId) {
            if(StringUtils.isEmpty(userId)){
                userId = "current_user_id";
            }
            return AjaxResult.success(todoService.allTodoData(userId));
    }
    
    /**
     * 根据ID查询待办事项
     */
    @RequestMapping("/{id}")
    public AjaxResult getTodo(@PathVariable Long id) {
        return AjaxResult.success(todoService.getTodoById(id)) ;
    }

    /**
     * 根据ID更新待办事项为完成窗台
     */
    @PostMapping("/complete")
    public AjaxResult completeTodo(@RequestBody TodoReq todo) {
        return AjaxResult.success(todoService.updateTodoComplete(todo)) ;
    }
    
    /**
     * 新增待办事项
     */
    @PostMapping
    public AjaxResult createTodo(@RequestBody TodoReq todo) {
        log.info("开始创建待办：{}",todo);
        Todo todoEntity = new Todo();
        BeanUtil.copyProperties(todo,todoEntity);
        int rows = todoService.createTodo(todoEntity);
        if (rows > 0) {
            return AjaxResult.success(todoService.getTodoById(todoEntity.getId()));
        } else {
            return AjaxResult.error("创建失败");
        }

    }
    
    /**
     * 修改待办事项
     */
    @PutMapping("/{id}")
    public AjaxResult updateTodo(@PathVariable Long id,
            @RequestBody TodoReq todo) {
            // 确保路径ID和对象ID一致
            todo.setId(id);
            
            if (todo.getTitle() == null || todo.getTitle().trim().isEmpty()) {
                return AjaxResult.error("任务标题不能为空");
            }
            
            // 检查待办事项是否存在
            Todo existingTodo = todoService.getTodoById(id);
            if (existingTodo == null) {
                return AjaxResult.error("待办事项不存在");
            }
            Todo todoEntity = new Todo();
            BeanUtil.copyProperties(todo,todoEntity);
            todoEntity.setUpdatedAt(LocalDateTime.now());
            return AjaxResult.success(todoService.updateTodo(todoEntity)) ;
    }
    
    /**
     * 删除待办事项
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteTodo(@PathVariable Long id) {
        // 检查待办事项是否存在
        Todo existingTodo = todoService.getTodoById(id);
        if (existingTodo == null) {
            return AjaxResult.error("待办事项不存在");
        }
        return AjaxResult.success(todoService.deleteTodo(id));
    }
    
    /**
     * 获取统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics(
            @RequestParam(required = false) String userId) {
        if (userId == null || userId.trim().isEmpty()) {
            userId = "current_user_id";
        }
        log.info("开始统计当天待办情况：{}",userId);
        return AjaxResult.success(todoService.getStatistics(userId));

    }


    /**
     * 获取统计信息
     */
    @GetMapping("/getStatisticsTask")
    public TodoTaskOutResp getStatisticsTask(
            @RequestParam(required = false) String userId) {
        if (userId == null || userId.trim().isEmpty()) {
            userId = "current_user_id";
        }
        log.info("开始统计当天待办情况：{}",userId);
        return todoService.getStatisticsTask(userId);

    }



}